Skip to main content

Java do-while loop

Banner java icon

🎒 Java Do-While Loop: The "At Least Once" Ride! πŸŽ‘β€‹

Ever wanted a loop that guarantees at least one execution, no matter what? Well, say hello to the do-while loopβ€”the thrill ride of Java loops! 🎠 Unlike its cautious cousin, the while loop, the do-while loop dives straight in and only checks if it should continue after the first run. πŸš€


🧐 The "Must-Know" Syntax​

Here’s how you write a do-while loop:

do {
// Execute this block at least once
} while (condition);

Key Takeaways πŸ“β€‹

  • The loop always runs at least onceβ€”no ifs, ands, or buts! πŸ™Œ
  • The condition is checked after execution, unlike a while loop.
  • Don't forget the semicolon ( ; ) after while (condition); or Java will give you the "silent treatment" (a.k.a. errors πŸ˜…).
  • Can be exited early with a break statement if needed.

🏁 Example: Counting from 1 to 5​

Here’s a simple example of a do-while loop in action:

int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);

Output:

1
2
3
4
5

Even if i started at 6, the loop would still print 6 once before checking the condition! 🀯


πŸ₯Š Do-While vs While: The Showdown​

What’s the real difference between while and do-while loops? Let's compare:

int i = -10;

// Simple while loop
while (i > 0) {
System.out.println(i); // Does not print anything!
i++;
}

// Do-while loop

do {
System.out.println(i); // Prints -10 and THEN exits!
i++;
} while (i > 0);

Key Difference πŸŽ―β€‹

  • while: Checks before executing β†’ Might never run ❌
  • do-while: Checks after executing β†’ Runs at least once βœ…

🚨 Watch Out for Infinite Loops! πŸš¨β€‹

If you forget to update your condition inside the loop, you might end up in an infinite loop. Like this... 😱

do {
System.out.println("This will never end... send help! 😡");
} while (true); // No exit condition!

Make sure to modify your condition variable inside the loop, or you’ll be staring at your terminal forever. πŸ˜‚


πŸŽ‰ Final Thoughts​

The do-while loop is perfect when you need to execute a block at least once, regardless of the condition. It's especially useful for: βœ… Menus that should display at least once πŸ“œ βœ… User input validation loops ⌨️ βœ… Cases where checking conditions before execution doesn’t make sense πŸ€”

Now go forth and loop responsibly! πŸ’»πŸ”₯

** Happy Coding! πŸš€**